# -*- coding: UTF-8 -*-
PRODUCTS = [["iphone", 6888],
            ["MacPro", 14800],
            ["Mi", 2499],
            ["Coffee", 31],
            ["Book", 60],
            ["Nike", 699]]


def go_shopping():
    shop_cart = []
    shop_cost = []

    while True:
        display_products()
        costumer_input = input("请输入购买的商品编号（输入q结束购买）：")
        if costumer_input == 'q':
            print(f"你已经完成购物, 请查看你的购物车清单！")
            display_cart(shop_cart, shop_cost)
            break

        product_id = int(costumer_input) \
            if costumer_input.isdigit() else -1

        if 0 <= product_id < len(PRODUCTS):
            product_name, product_price = PRODUCTS[int(costumer_input)]
            shop_cart.append(product_name)
            shop_cost.append(product_price)
            print(f'你已将{product_name}加入购物车, 查看购物车清单后继续选购商品！')
            display_cart(shop_cart, shop_cost)
        else:
            print("输入有误请重新输入，请重新购物！")


def display_products():
    print("欢迎来到马哥小店！")
    print("------ 商品列表 ------")
    print("商品编号    商品名称    商品价格")
    for product_id, product in enumerate(PRODUCTS, 0):
        product_name, product_price = product
        print(f'{product_id}    {product_name}    {product_price}')

    print("-" * 16)


def display_cart(shop_cart, shop_cost):
    print("------    购物车清单    ------")
    print("商品名称    商品价格")

    total_price = 0
    for product in zip(shop_cart, shop_cost):
        product_name, product_price = product
        total_price += product_price
        print(f"{product_name}    {product_price}")

    print(f"购物车总金额为{total_price}元")
    print("-" * 25)


if __name__ == '__main__':
    # 开始购物
    go_shopping()
